<?

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			includes/text.php
#	FUNCTION:		Functions for handling text
#	AUTHOR:			Cameron Morrow
#	CREATED:		27/07/2005
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Just letters
$VALID_CHARS_LETTERS = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");

$VALID_CHARS_LETTERS_UPPER = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");

# Just numbers
$VALID_CHARS_NUMBERS = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");

# Valid chars for use in IDs etc
$VALID_CHARS_ID = array_merge($VALID_CHARS_LETTERS, $VALID_CHARS_NUMBERS, array("_", "-"));

# Valid chars for use in Names etc
$VALID_CHARS_NAME = array_merge($VALID_CHARS_LETTERS, $VALID_CHARS_NUMBERS, array("_", "-", "'", "\\", " ", "(", ")"));

$VALID_CHAR_LIST = array(
	"numbers" => $VALID_CHARS_NUMBERS,
	"numbersspaces" => array_merge($VALID_CHARS_NUMBERS, array(" ", "(", ")")),
	"ids" => $VALID_CHARS_ID,
	"idsspaces" => array_merge($VALID_CHARS_ID, array(" ")),
	"names" => $VALID_CHARS_NAME,
	"namesupper" => array_merge($VALID_CHARS_NAME, $VALID_CHARS_LETTERS_UPPER),
	"email" => array_merge($VALID_CHARS_ID, array("@", ".")),
	"pagenames" => array_merge($VALID_CHARS_NAME, $VALID_CHARS_LETTERS_UPPER, array("?", "!", ":", ",")),
	"any" => array()
);

/**
 * containsValidChars() checks the passed string to ensure it contains only chars from the passed array
 *
 * @param check_string is the string to check
 * @param valid_chars is the array of valid chars
 *
 * @return whether it is valid or not
*/
function containsValidChars($check_string = "", $valid_chars = array()) {

	# Check for validity
	$valid = true;

	# For each character
	for ($i = 0; $i < strlen($check_string); $i++) {

		# Check if it's in the valid chars
		if (findInArray($valid_chars, substr($check_string, $i, 1)) === false) {
			$valid = false;
		}
	}

	# Return
	return $valid;
}


/**
 * generateRandomPassword() generates a random password
 *
 * @param length is the length of the password to create
 *
 * @return the password
*/
function generateRandomPassword($length = 8) {

	global $VALID_CHARS_LETTERS;
	global $VALID_CHARS_LETTERS_UPPER;
	global $VALID_CHARS_NUMBERS;

	$valid_chars = array_merge($VALID_CHARS_LETTERS, $VALID_CHARS_NUMBERS);

	$password = "";

	for ($i = 0; $i < $length; $i++) {

		$password .= $valid_chars[rand(0, count($valid_chars) - 1)];
	}

	return $password;
}


/**
 * removeTags() completely removes any tags
 *
 * @param input is the input
 *
 * @return the untagged string
*/
function removeTags($input = "") {

	# Remove any tags
	$untagged = "";
	$in_tags = false;
	$end_tag_after_next = false;

	# For each char
	for ($i = 0;  $i < strlen($input); $i++) {

		# If opening a tag
		if ($input[$i] == "<") {
			$in_tags = true;
		}

		# If closing a tag
		if ($i > 0 && $in_tags) {
			if ($input[$i] == ">") {
				$end_tag_after_next = true;
			}
		}

		# As long as we are not in a tag, add
		if (!$in_tags) {
			$untagged .= $input[$i];
		}

		# If ending a tag, finish now
		if ($end_tag_after_next) {
			$end_tag_after_next = false;
			$in_tags = false;
			if (strlen($untagged) > 0) {
				if ($untagged[strlen($untagged) - 1] != " ") {
					$untagged .= " ";
				}
			}
		}
	}

	# Return
	return $untagged;
}


/**
 * removeInvalidChars() removes all chars in a string that are not in the passed array of valid chars
 *
 * @param in_string is the string to parse
 * @param valid_chars is the array of valid chars
 *
 * @return the string with bad chars removed
*/
function removeInvalidChars($in_string = "", $valid_chars = array()) {

	# Create output string
	$out_string = "";

	# For each char
	for ($i = 0; $i < strlen($in_string); $i++) {

		# Check if it's in the valid chars
		if (findInArray($valid_chars, $in_string[$i]) !== false) {
			$out_string .= $in_string[$i];
		}
	}

	# Return
	return $out_string;
}


/**
 * better_strrpos is a version of strrpos that will work with searches of >1 char on PHP 4 and lower
 *
 * @param haystack is the haystack
 * @param needle is the needle
 *
 * @return the index
*/
function better_strrpos($haystack = "", $needle = "") {

	if (strpos(strrev($haystack), strrev($needle)) !== false) {

		# Flip both strings around and search, then adjust position based on string lengths
		return strlen($haystack) - strlen($needle) - strpos(strrev($haystack), strrev($needle));

	} else {

		return false;
	}
}


/**
 * createSelector() creates an HTML <select> with appropriate options
 *
 * @param select_name is the id of the selector
 * @param optionA is an array of the options
 * @param optionValueA is an array of option values, or "num" to use number values
 * @param selected_index is the item to select
 *
 * @return the ready-to-display selector
*/
function createSelector($select_name = "", $option_array = array(), $option_value_array = array(), $selected_index = 0, $javascript_function = "") {

	# Start
	$output = "<select id=\"" . $select_name . "\" name=\"" . $select_name . "\"" . ($javascript_function != "" ? "onchange=\"" . $javascript_function . "\"" : "") . ">\n";

	# For each option
	foreach ($option_array as $key => $option_text) {

		$output .= ($option_value_array == "num") ? "<option value=\"" . $key . "\"" : "<option value=\"" . $option_value_array[$key] . "\"";
		
		$output .= ($key == $selected_index) ? " selected=\"selected\"" : "";
		
		$output .= ">" . $option_text . "</option>";
	}

	# End
	$output .= "</select>\n";

	# Return
	return $output;
}


/**
 * createRadioButtonPair() creates a pair of radio buttons, for use in Yes/No, On/Off etc situations
 *
 * @param button_id is the ID of the radio button pair
 * @param no_option is the text to use for the zero option
 * @param yes_option is the text to use for the one option
 * @param selected_index is the default button to use
 *
 * @return the HTML ready to display
*/
function createRadioButtonPair($button_id = "", $no_option = "No", $yes_option = "Yes", $selected_index = 0) {

	# First Button
	$output = "<input type=\"radio\" name=\"" . $button_id . "\" id=\"" . $button_id . "_0\" value=\"0\"" . (($selected_index == 0) ? " checked=\"checked\"" : "") . " /> <label for=\"" . $button_id . "_0\">" . $no_option . "</label><br />";

	# Second Button
	$output .= "<input type=\"radio\" name=\"" . $button_id . "\" id=\"" . $button_id . "_1\" value=\"1\"" . (($selected_index == 1) ? " checked=\"checked\"" : "") . " /> <label for=\"" . $button_id . "_1\">" . $yes_option . "</label>";

	# Return
	return $output;
}


/**
 * createCheckboxList() creates a list of checkboxes
*/
function createCheckboxList($button_id = "", $name_array = array(), $id_array = "num", $checked_array = array()) {

	$output = "";

	foreach ($name_array as $counter => $name) {

		$id = ($id_array == "num" || !@$id_array[$counter]) ? $counter : @$id_array[$counter];
		$checked = (@array_search($id, $checked_array) !== false) ? " checked=\"checked\"" : "";

		$output .= "<input type=\"checkbox\" name=\"" . $button_id . $id . "\" id=\"" . $button_id . $id . "\"" . $checked . " /> <label for=\"" . $button_id . $id . "\">" . $name . "</label>";
		
		if ($counter < count($name_array) - 1) {
			$output .= "<br />";
		}

	}

	return $output;
}

/**
 * generateActionForm() generates the action form used in the admin
 *
 * @return the HTML ready to print
*/
function generateActionForm() {

	# Global
	global $PAGE_ID;

	# Form
	$output = "<form name=\"actionform\" id=\"actionform\" action=\"" . $PAGE_ID . ".php\" method=\"post\">" .
		"<input type=\"hidden\" name=\"mode\" id=\"mode\" value=\"\" />" .
		"<input type=\"hidden\" name=\"id\" id=\"id\" value=\"\" />" .
		"</form>";

	# Return
	return $output;
}


/**
 * generateAddLink() generates a button that can be clicked to go to the add form
*/
function generateAddLink($add_type = "item") {

	# Global
	global $PAGE_ID;

	# Form
	$output = "<form name=\"addform\" id=\"addform\" action=\"" . $PAGE_ID . ".php\" method=\"post\">" .
		"<p><input type=\"button\" onclick=\"doAction('add', '', false);\" value=\"Add a new " . $add_type . "\" /></p>" .
		"</form>";

	# Return
	return $output;
}


/**
 * generateAddLinkForNewsletters() generates a special add button for the 'Add Newsletter' functionality
*/
function generateAddLinkForNewsletters($add_type = "item") {

	# Global
	global $PAGE_ID;

	# Form
	$output = "<form name=\"addform\" id=\"addform\" action=\"" . $PAGE_ID . ".php\" method=\"post\">" .
		"<p><input type=\"button\" onclick=\"getNewNewsletterName();\" value=\"Add a new " . $add_type . "\" /></p>" .
		"</form>";

	# Return
	return $output;
}


/**
 * generateAddEditColumn() generates an Add/Edit links column for the browse tables
*/
function generateAddEditColumn($index_field, $show_edit = true, $show_delete = true) {
	
	# Create output
	$output = "";

	# If an edit link should be shown
	if ($show_edit) {

		$output .= "\"<a href=\\\"javascript:doAction('edit', '[" . $index_field . "]', false);\\\">Edit</a>";
		
	} else {
		$output .= "\"";
	}
	
	if ($show_edit && $show_delete) {
		$output .= " / ";
	}
	
	if ($show_delete) {
		$output .= "<a href=\\\"javascript:doAction('delete', '[" . $index_field . "]', true);\\\">Delete</a>\"";
	} else {
		$output .= "\"";
	}

	return array("Actions", $output);
}


/**
 * generatePageMoveFlash() generates the page move flash file HTML
 *
 * @param flash_url is the path to use for the flash file
 * @param flash_width is the width
 * @param flash_height is the height
 *
 * @return the HTML ready to be written
*/
function generatePageMoveFlash($flash_url = "../assets/flash/page_move_01.swf", $flash_width = 300, $flash_height = 550) {

	# Generate
	$output = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\" width=\"" . $flash_width . "\" height=\"" . $flash_height . "\" id=\"pagemove\">" .
		"<param name=\"allowScriptAccess\" value=\"sameDomain\" />" .
		"<param name=\"scale\" value=\"noscale\" />" .
		"<param name=\"movie\" value=\"" . $flash_url . "\" />" .
		"<param name=\"quality\" value=\"high\" />" .
		"<param name=\"bgcolor\" value=\"#ffffff\" />" .
		"<embed src=\"" . $flash_url . "\" scale=\"noscale\" quality=\"high\" bgcolor=\"#ffffff\" width=\"" . $flash_width . "\" height=\"" . $flash_height . "\" id=\"pagemove\" name=\"pagemove\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />" .
		"</object>";

	# Return
	return $output;
}


/**
 * generatePageEditFlash() generates the page edit flash file HTML
 *
 * @param flash_url is the path to use for the flash file
 * @param flash_width is the width
 * @param flash_height is the height
 *
 * @return the HTML ready to be written
*/
function generatePageEditFlash($flash_url = "../assets/flash/editor3_02.swf", $flash_width = 800, $flash_height = 600) {

	# Generate
	$output = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\" width=\"" . $flash_width . "\" height=\"" . $flash_height . "\" id=\"pagemove\">" .
		"<param name=\"allowScriptAccess\" value=\"sameDomain\" />" .
		"<param name=\"scale\" value=\"noscale\" />" .
		"<param name=\"movie\" value=\"" . $flash_url . "\" />" .
		"<param name=\"quality\" value=\"high\" />" .
		"<param name=\"bgcolor\" value=\"#ffffff\" />" .
		"<embed src=\"" . $flash_url . "\" scale=\"noscale\" quality=\"high\" bgcolor=\"#ffffff\" width=\"" . $flash_width . "\" height=\"" . $flash_height . "\" id=\"pagemove\" name=\"pagemove\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />" .
		"</object>";

	
	# Return
	return $output;
}

#Gerenate Page form for admin pages
function genertatePageForm($Group){
	
	global $PROJECT_DB_TABLES;
	
	$output = "";
	
	$output .= "<div id=\"DIV_ADMINPAGES_CONTENT\" align=\"left\">";
	
	$output .= "<div id=\"DIV_ADMINPAGES_LEFT\">";
	
	$output .= "<table cellspacing=\"0\" align=\"left\">";
	
	$sql = "SELECT distinct p_section FROM ".$PROJECT_DB_TABLES["pages"]." order by p_id,p_sort";
	
	$current_data = getRecord($sql);
	
	foreach($current_data as $current_key=>$current_value){
		
		$output.="<tr><td valign=\"top\" style=\"border-color: white;border-style: solid;\"><img id=\""."img_".$current_value["p_section"]."\" src=\"../assets/images/tree/folder_closed.png\" onclick=\"ExpandParentPage('".$current_value["p_section"]."','');\"/></td><td style=\"border-color: white;border-style: solid;\">".$current_value["p_section"]."<br/><div id=\""."div_".$current_value["p_section"]."\"></div></td></tr>";
	}//".$current_value["p_id"]."
	
	if ($Group == 'Developers'){
		
		$junkbox_id = 'junkbox';
		
		$output.= "<tr><td valign=\"top\" style=\"border-color: white;border-style: solid;\"><img id=\"img_".$junkbox_id."_junk\" src=\"../assets/images/tree/folder_closed.png\" onclick=\"ExpandParentPage('".$junkbox_id."','');\"/></td><td style=\"border-color: white;border-style: solid;\">".$junkbox_id."<br/><div id=\""."div_".$junkbox_id."_junk\"></div></td></tr>";
		
	}
	$output .= "</table>";
	
	$output .= "</div>";
	
	$output .= "<div id=\"DIV_ADMINPAGES_RIGHT\">";
	
	$output .= "</div>";
	
	$output .= "</div>";
	
	return $output;
}

#Gerenate Page move form 
function genertatePageMoveForm($parent = "", $section = ""){
	
	global $SubDirContent;
	
	global $pagePath;
	
	global $pagesMove;
	
	global $count;
	
	$pagesChildren = $pagesMove -> getChildrenOfPage($parent);
	
	$SubDirContent.= "<table cellspacing=\"0\" align=\"left\">";
	
	foreach ($pagesChildren -> getPageIDs() as $pageID){
		
		$obPage = $pagesChildren -> getPage($pageID);#Get page object
		
		$obPageSection = $obPage -> getData("p_section");#Get page section
		
		$obPageId = $obPage -> getData("p_id");#Get page id
		
		$obPageName = $obPage -> getData("p_name");#Get page name 
		
		if($obPageSection == $section){
			
		  $obPageChildrenCon =  count($pagesMove -> getChildrenOfPage($obPageId) -> getPageIDs());#The mount of children 
		
		  if($pagePath[$count] == $obPageId){ 
			  
			  if($count != count($pagePath)-1){
			  
			  		if($obPageChildrenCon == 0){
			  
			  	 		$SubDirContent.="<tr><td valign=\"top\" class=\"movetd\"><img id=\""."img_".$obPageId."\" src=\"../assets/images/tree/document.png\");\"/></td><td class=\"movetd1\" align=\"left\">";
			  		}
			  		else{
				 
				 		$SubDirContent.="<tr><td valign=\"top\" class=\"movetd\"><img id=\""."img_".$obPageId."\" src=\"../assets/images/tree/folder_open.png\");\" onclick=\"ExpandPage('".$obPageId."',false,true);\"/></td><td class=\"movetd1\" align=\"left\">"; 
			  		}
			  
			  		$SubDirContent.="<a id=\"a_".$obPageId."\" onclick=\"\" style=\"text-decoration:none;\" href=\"javascript:itemSelected('".$obPageId."','".$parent."','".$section."');\">".$obPageName."(".$obPageId.")"."</a><br/><div align=\"left\" id=\""."div".$obPageId."\">";
			  
			  		$count ++;
					
					genertatePageMoveForm($obPageId,$section);
			  }
			  else{
				  
				    if($obPageChildrenCon == 0){
			  
			  	 		$SubDirContent.="<tr><td valign=\"top\" class=\"movetd\"><img id=\""."img_".$obPageId."\" src=\"../assets/images/tree/document.png\");\"/></td><td class=\"movetd1\" align=\"left\">";
			  		}
			  		else{
				 
				 		$SubDirContent.="<tr><td valign=\"top\" class=\"movetd\"><img id=\""."img_".$obPageId."\" src=\"../assets/images/tree/folder_closed.png\" onclick=\"ExpandPage('".$obPageId."',false,false);\");\"/></td><td class=\"movetd1\" align=\"left\">"; 
			  		}
			  
			  		$SubDirContent.="<a id=\"a_".$obPageId."\" onclick=\"\" style=\"text-decoration:none;\" href=\"javascript:itemSelected('".$obPageId."','".$parent."','".$section."');\">".$obPageName."(".$obPageId.")"."</a><br/><div align=\"left\" id=\""."div".$obPageId."\">";
			  }
			  
			  $SubDirContent .= "</div></td></tr>";
		  }
		  else{
			  
			  if($obPageChildrenCon == 0){
			  
			  	 	$SubDirContent.="<tr><td valign=\"top\" class=\"movetd\"><img id=\""."img_".$obPageId."\" src=\"../assets/images/tree/document.png\");\"/></td><td class=\"movetd1\" align=\"left\">";
			  }
			  else{
				 
				 	$SubDirContent.="<tr><td valign=\"top\" class=\"movetd\"><img id=\""."img_".$obPageId."\" src=\"../assets/images/tree/folder_closed.png\");\" onclick=\"ExpandPage('".$obPageId."',false,false);\"/></td><td class=\"movetd1\" align=\"left\">"; 
			  }
			  
			  $SubDirContent.="<a id=\"a_".$obPageId."\" onclick=\"\" style=\"text-decoration:none;\" href=\"javascript:itemSelected('".$obPageId."','".$parent."','".$section."');\">".$obPageName."(".$obPageId.")"."</a><br/><div align=\"left\" id=\""."div".$obPageId."\">";
			  
			  $SubDirContent .= "</div></td></tr>";
		  }
		}
	}
	
	$SubDirContent.= "</table>";
}

/**
 * generatePageSelectFlashInput() function is used only for the page edit model and page create model for user to select parent of page
 *
 * @return the output
*/
function generatePageSelectFlashInput($current_parent="",$current_section="") {
	
	# Globals
	global $PASSED_ID;
	
	global $PROJECT_DB_TABLES;

	# Output
	$output = "<tr class=\"norightborder\"><td>Select Parent:</td>";

	# Flash URL
	$flash_url = $flash_filename = "../assets/flash/page_move_01.swf?mode=select&host=../&passed_width=300&passed_height=300&selected_page_id=parentof:" . $PASSED_ID;

	# Add flash page selector
	$output .= "<td align=\"left\">";//. generatePageMoveFlash($flash_url, 300, 300);
	
	# Add Hidden Fields
	$output .= "<input type=\"text\" name=\"p-parent\" id=\"p-parent\" value=\"".$current_parent."\" />";
	
	$output .= "<input type=\"text\" name=\"p-section\" id=\"p-section\" value=\"".$current_section."\" />";
	
	#Test HTML PAGE
	$output .= "<div align=\"left\" style=\"height:500px;width:100%;overflow:scroll;background-color:#FFFFFF;\">";
	
	$output .= "<table cellspacing=\"0\" align=\"left\">";
	
	$sql="SELECT distinct p_section FROM ".$PROJECT_DB_TABLES["pages"]." order by p_id,p_sort";
	
	$current_data=getRecord($sql);
	
	foreach($current_data as $current_key=>$current_value){
		
		$output.="<tr><td valign=\"top\" style=\"border-color: white;border-style: solid;\"><img id=\""."img_".$current_value["p_section"]."\" src=\"../assets/images/tree/folder_closed.png\" onclick=\"ExpandPage('".$current_value["p_section"]."',true,false);\"/></td>";
		
		$output.="<td style=\"border-color: white;border-style: solid;\"><a id=\"a_".$current_value["p_section"]."\" onclick=\"\" href=\"javascript:foo('','".$current_value["p_section"]."');\" style=\"text-decoration:none;\">".$current_value["p_section"]."</a><br/><div id=\""."div".$current_value["p_section"]."\"></div></td></tr>";
	}//".$current_value["p_id"]."
	
	$output .= "</table>";
	
	$output .= "</div>";
	
	$output .= "<div>";
	
	$output .= "SELECTED ITEM:<label id=\"lblSelected\"></label>";
	
	$output .= "</div>";
	
	# Close
	$output .= "</td></tr>";

	return $output;

}


/**
 * parseFlashXML() parses the flash XML into HTML
 *
 * @param flash_xml is the flash XML
 *
 * @return the ready to print HTML
*/
function parseFlashXML($flash_xml = "") {

	# Check it is valid XML
	if (substr($flash_xml, 0, 5) != "<?xml") {

		# Return unformatted
		return $flash_xml;
	}

	# Before we parse the XML, grab any XHTML blocks and store them to be dumped back in later
	$xhtml_blocks = array();
	$flash_xml = explode("<HTMLTextbox display=\"", $flash_xml);
	foreach ($flash_xml as $fc => $xml) {
		if ($fc > 0) {
			$prefix = substr($xml, 0, strpos($xml, ">") + 1);
			$xhtml = substr($xml, 0, strpos($xml, "<"));
			$xhtml = substr($xhtml, strlen($prefix));
			$xhtml_blocks[] = flash_unescape($xhtml);
			$suffix = substr($xml, strlen($prefix) + strlen($xhtml));

			$flash_xml[$fc] = $prefix . "{{XHTML" . (count($xhtml_blocks) - 1) . "}}" . $suffix;
		}
	}
	$flash_xml = implode("<HTMLTextbox display=\"", $flash_xml);


	#$save = createFile("../includes/", "parse-log-" . getFullDate() . "-" . rand(100, 999) . ".txt", $flash_xml);

	$flash_xml = flash_unescape($flash_xml);
	$flash_xml = str_replace(
		array("<HTMLTextbox display=\"block\">", "<HTMLTextbox display=\"inline\">", "</HTMLTextbox>"),
		array("<HTMLTextbox display=\"block\"><![CDATA[", "<HTMLTextbox display=\"inline\"><![CDATA[", "]]></HTMLTextbox>"),
		$flash_xml);

	# Run through XSL parser
	$arguments_flash = array(
		"/_xml" => $flash_xml,
		"/_xsl" => file_get_contents("../includes/flash/html.xsl")
	);

	# Create XSLT Processor
	$xsltproc_flash = xslt_create();

	# Convert
	$flash_content = xslt_process(
		$xsltproc_flash,
		'arg:/_xml',
		'arg:/_xsl',
		null,
		$arguments_flash
	);

	# Finish
	xslt_free($xsltproc_flash);

	#$save = createFile("../includes/", "parse1-log-" . getFullDate() . "-" . rand(100, 999) . ".txt", $flash_content);

	# Splice XHTML back in
	$flash_content = preg_replace(
		"/\{\{XHTML([0-9]*)\}\}/ie",
		"$" . "xhtml_blocks[\\1]",
		$flash_content);

	# Remove <htmltextbox> tags
	$flash_content = preg_replace(
		array("/<\/*htmltextbox[^>]*>/i", "/>\ *</i"),
		array("", "><"),
		$flash_content);

	# Fix up any dodgy chars
	$flash_content = str_replace(
		array(
			"\r",
			"\t",
			"\n",
			"<br>",
			"    ",
			"<p></p>"
		),
		array(
			"",
			"",
			"",
			"<br />",
			"",
			"<p>&nbsp;</p>"
		),
		$flash_content);
	

	# Parse columns
	$column_id = 1;
	$column_code = "class=\"column\">";
	while (strpos($flash_content, $column_code) !== false && $column_id < 3) {
		$index = strpos($flash_content, $column_code);
		$flash_content = substr($flash_content, 0, $index) . "class=\"column\" id=\"column" . $column_id . "\">" . substr($flash_content, $index + strlen($column_code));
		$column_id++;
	}

	# Return
	return $flash_content;

}


/**
 * parseFlashLinks() parses flash links into HTML links
 *
 * @param html is the HTML to parse
 *
 * @return the parsed html
*/
function parseFlashLinks($html = "") {

	return preg_replace(
		array("/<a href\=\"asfunction\:editLink\,([^\,]*)\,[^\"]*\"/i"),
		array("<a href=\"\\1\""),
		$html);
}


/**
 * deTag() replaces < > and their HTML entities
 *
 * @param input is the input
 *
 * @return the replaced output
*/
function deTag($input) {

	return preg_replace(
		array("/</i", "/>/i"),
		array("&lt;", "&gt;"),
		$input);
}


/**
 * formatBytes() formats an amount in bytes
 *
 * @param bytes is the value in bytes
 *
 * @return the formatted output
*/
function formatBytes($bytes) {

	# Convert to integer
	$bytes = (integer) $bytes;

	if ($bytes >= 1099511627776) {
		$return = round($bytes / 1024 / 1024 / 1024 / 1024, 2);
		$suffix = "TB";
	} else if ($bytes >= 1073741824) {
		$return = round($bytes / 1024 / 1024 / 1024, 2);
		$suffix = "GB";
	} else if ($bytes >= 1048576) {
		$return = round($bytes / 1024 / 1024, 2);
		$suffix = "MB";
	} elseif ($bytes >= 1024) {
		$return = round($bytes / 1024, 2);
		$suffix = "KB";
	} else {
		$return = $bytes;
		$suffix = "Byte";
	}

	# Check if single or plural
	if ($return == 1 || $suffix != "Byte") {
		$return .= " " . $suffix;
	} else {
		$return .= " " . $suffix . "s";
	}

	# Return
	return $return;
}


/**
 * adminPagePrintout() is a custom handler for printing the admin page browser
 *
 * @param current_pages is an array of pages
 * @param parent_id is the ID of the parent to show the pages in
 * @param section is the section to be of
*/
function adminPagePrintout($current_pages = array(), $parent_id = "", $section = "site", $depth = 0) {

	global $ROW_COUNT;
	if (!$ROW_COUNT) {
		$ROW_COUNT = 0;
	}

	foreach ($current_pages as $current_page_id => $current_page) {

		if ($current_page["p_parent"] == $parent_id && $current_page["p_section"] == $section && $current_page["p_status"]<3) {
				$padding = "";
				for ($i = 0; $i < $depth; $i++) {
					$padding .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
				}
	
				addContent("<tr" . ($ROW_COUNT % 2 == 0 ? " class=\"alternate\"" : "") . ">" .
					"<td>" . $padding . "<a href=\"../" . $current_page["p_section"] . "/" . $current_page_id . ".php\">" . $current_page["p_name"] . "</a> (" . $current_page_id . ")</td>" .
					"<td>" . getPageStatusName($current_page["p_status"]) . "</td>" .
					"<td><a href=\"javascript:launchPageMove('" . $current_page_id . "');\">Move</a></td>" .
					"<td><a href=\"javascript:launchPagePreview('" . $current_page_id . "');\">Preview</a></td>" .
					"<td><a href=\"javascript:editPageContent('" . $current_page_id . "');\">Content</a></td>" .
					"<td><a href=\"javascript:doAction('edit', '" . $current_page_id . "', false);\">Edit</a> / <a href=\"javascript:doAction('remove', '" . $current_page_id . "', true);\">Remove</a></td></tr>");
	
				$ROW_COUNT++;
	
				#addContent("<p>" . $depth . ": Page: " . $current_page_id . " (Parent: " . $current_page["p_parent"] . ")</p>");
	
				adminPagePrintout($current_pages, $current_page_id, $section, $depth + 1);
		}
	}
}

/**
 * adminPageJunkPrintOut() is a custom handler for printing the admin page browser
 *
 * @param current_pages is an array of pages
 * @param parent_id is the ID of the parent to show the pages in
 * @param section is the section to be of
*/
function adminPageJunkPrintOut($current_pages = array()){
		foreach ($current_pages as $current_page_id => $current_page) {

		if ($current_page["p_status"]==3) {
	
				addContent("<tr>" .
					"<td>".$current_page_id."</td>" .
					"<td>" . getPageStatusName($current_page["p_status"]) . "</td>" .
					"<td><a href=\"javascript:doAction('restore', '" . $current_page_id . "', false);\">Restore</a></td></tr>");		
				#addContent("<p>" . $depth . ": Page: " . $current_page_id . " (Parent: " . $current_page["p_parent"] . ")</p>");
		}
	}
}

/**
 * adminUserPrintOut() is a custom handler for printing the admin page browser
 *
 * @param current_pages is an array of pages
 * @param parent_id is the ID of the parent to show the pages in
 * @param section is the section to be of
*/
function adminUserPrintOut($current_users = array(), $current_groups=array(), $section=""){

	foreach ($current_users as $current_user_id => $current_user) {
			if($section!="Others")
			{
				if($current_user["g_name"]==$section){

					addContent("<tr>" .
					"<td>" . getGroupName1($current_user["u_group"],$current_groups). "</td>".
					"<td>" . $current_user["u_id"] . "</td>".
					"<td>" . $current_user["u_login_name"] . "</td>".
					"<td>" . formatDate($current_user["u_created"]) . "</td>".
					"<td>" . formatDate($current_user["u_lastlogin"]) . "</td>".
					"<td><a href=\"javascript:openChangePasswordAdminWin('" . $current_user_id . "');\">Change Password</a></td>" .
					"<td><a href=\"javascript:doAction('edit', '" . $current_user_id . "', false);\">Edit</a> / <a 		href=\"javascript:doAction('delete', '" . $current_user_id . "', true);\">Delete</a></td></tr>");
				}
			}
			else{
				if($current_user["g_name"]!="Developers" && $current_user["g_name"]!="Administrators"){
					addContent("<tr>" .
					"<td>" . getGroupName1($current_user["u_group"],$current_groups). "</td>".
					"<td>" . $current_user["u_id"] . "</td>".
					"<td>" . $current_user["u_login_name"] . "</td>".
					"<td>" . formatDate($current_user["u_created"]) . "</td>".
					"<td>" . formatDate($current_user["u_lastlogin"]) . "</td>".
					"<td><a href=\"javascript:openChangePasswordAdminWin('" . $current_user_id . "');\">Change Password</a></td>" .
					"<td><a href=\"javascript:doAction('edit', '" . $current_user_id . "', false);\">Edit</a> / <a 		href=\"javascript:doAction('delete', '" . $current_user_id . "', true);\">Delete</a></td></tr>");
				}
			}
	}
}

# Implement equivalents of Flash's 'escape' and 'unescape' functions
function flash_escape($input) {
	return rawurlencode($input);
}

function flash_unescape($input) {
	return rawurldecode($input);
}

function back($input) {
	return $input;
}

?>